package labox.innovation.gameserver.model.actor.knownlist;

import labox.innovation.gameserver.model.L2Object;
import labox.innovation.gameserver.model.SceneRegion;

public class ObjectKnownList {

	protected L2Object _activeObject;

	private SceneRegion[] _knownObjects;

	// =========================================================
	// Constructor
	public ObjectKnownList(L2Object activeObject) {
		_activeObject = activeObject;
	}

	public boolean addKnownObject(L2Object object) {
		// 副本ID是-1是为GM设计的，能看到所有的副本中发生的所有东西
		//如果是与此对象不同的副本的，则不添加
		int id = _activeObject.getInstanceId();
		if (id != -1 && object.getInstanceId() != id)
			return false;
		return true;
	}

	public boolean removeKnownObject(L2Object object) {
		//如果是与此对象不同的副本的，则不删除
		int id = _activeObject.getInstanceId();
		if (id != -1 && object.getInstanceId() != id)
			return false;
		return true;
	}

	public boolean addKnownRegion(int index, SceneRegion region) {
		if (getKnownRegions()[index] != null) {
			for (L2Object object : getKnownRegions()[index].getVisibleObjects().values()) {
				object.getKnownList().removeKnownObject(_activeObject);
				removeKnownObject(object);
			}
		}
		getKnownRegions()[index] = region;
		if (region != null) {
			for (L2Object object : region.getVisibleObjects().values()) {
				object.getKnownList().addKnownObject(_activeObject);
				addKnownObject(object);
			}
		}
		return true;
	}

	/** Remove all L2Object from _knownObjects */
	public void removeAllKnownRegion() {
		SceneRegion[] regions = getKnownRegions();
		for (int i = -1; ++i < regions.length;) {
			addKnownRegion(i, null);
		}
	}

	public boolean removeKnownRegion(SceneRegion object) {
		if (object == null)
			return false;
		SceneRegion[] regions = getKnownRegions();
		for (int i = -1; ++i < regions.length;) {
			if (regions[i] == object) {
				return addKnownRegion(i, null);
			}
		}
		return false;
	}

	// =========================================================
	// Property - Public
	public L2Object getActiveObject() {
		return _activeObject;
	}

	public int getDistanceToForgetObject(L2Object object) {
		return 0;
	}

	public int getDistanceToWatchObject(L2Object object) {
		return 0;
	}

	/** Return the _knownObjects containing all L2Object known by the L2Character. */
	public final SceneRegion[] getKnownRegions() {
		if (_knownObjects == null)
			_knownObjects = new SceneRegion[9];
		return _knownObjects;
	}

}
